<?php

class PhoneAction extends UserAction
{
	public function _initialize()
	{
		parent::_initialize();
		$this->canUseFunction('Phone');
		
	}

	public function index()
	{
		$this->baseSet();
	}

	public function baseSet()
	{
		$info = M('Mobilesite')->where(array('token' => session('token')))->find();
		if (is_array($info) && !empty($info['tjscript'])) {
			$info['tjscript'] = base64_decode($info['tjscript']);
			$info['tjscript'] = urldecode(str_replace('jshtmltag', 'script', $info['tjscript']));
		}

		$autodomain = '';
		$mbid = (is_array($info) && (0 < $info['id']) ? $info['id'] : 0);

		if (in_array(C('server_topdomain'), array('yunweicms.com', 'yunweicms.com'))) {
			$autodomain = 'm_' . session('token') . '.yunweicms.com';
		}

		if (!empty($autodomain)) {
			$nomodify = true;
		}
		else {
			$nomodify = false;
		}

		$this->assign('infos', $info);
		$this->assign('autodomain', $autodomain);
		$this->assign('nomodify', $nomodify);
		$this->assign('mbid', $mbid);
		$this->display('baseSet');
	}

	public function saveData()
	{
		$id = $this->_post('id', 'trim');
		$owndomain = $this->_post('dnm', 'trim');

		if ($owndomain == 'nomodify') {
			$owndomain = 'm_' . session('token') . '.yunweicms.com';
		}
		else {
			if (empty($owndomain) || preg_match('/[^A-Za-z0-9_\\.\\-]/', $owndomain)) {
				$this->dexit(array('error' => 1, 'msg' => '域名格式不对'));
			}
		}

		$tjscript = $this->_post('tjscr', 'trim');

		if (!empty($tjscript)) {
			$tjscript = str_replace('script', 'jshtmltag', $tjscript);
			$tjscript = base64_encode($tjscript);
		}
		else {
			$tjscript = '';
		}

		$token = trim(session('token'));
		$db_mobilesite = M('Mobilesite');
		$tmps = $db_mobilesite->where(array('owndomain' => $owndomain))->select();

		if (!empty($tmps)) {
			if (1 < count($tmps)) {
				$this->dexit(array('error' => 1, 'msg' => '此域名已经被绑定过了'));
			}
			else if ($tmps[0]['token'] != $token) {
				$this->dexit(array('error' => 1, 'msg' => '此域名已经被绑定过了'));
			}
		}

		$tmp = $db_mobilesite->where(array('id' => $id, 'token' => $token))->find();
		if (is_array($tmp) && !empty($tmp)) {
			$db_mobilesite->where(array('id' => $tmp['id']))->save(array('tjscript' => $tjscript, 'owndomain' => $owndomain));
			$this->dexit(array('error' => 0, 'msg' => $tmp['id']));
		}
		else {
			$data['token'] = $token;
			$data['owndomain'] = $owndomain;
			$data['admindomain'] = $_SERVER['HTTP_HOST'];
			$data['tjscript'] = $tjscript;
			$data['addtime'] = time();
			$mbid = $db_mobilesite->add($data);

			if (0 < $mbid) {
				$this->dexit(array('error' => 0, 'msg' => $mbid));
			}
		}

		$this->dexit(array('error' => 1, 'msg' => '操作失败'));
	}

	public function downloadFile()
	{
		$tjscript = base64_encode($this->_get('tjscr', 'trim'));
		$content = '<?php' . "\r\n" . '	/* * *服务器必须要安装php libcurl功能扩展库（Client URL Library）****** */' . "\r\n" . '	/* * *将此文件放在站点根目录下并改名成 index.php即可****** */' . "\r\n" . '	/* * *Author LiHongShun**2015-01-07**** */' . "\r\n" . '	/* * *这种方式 cookie存在跨域获取不到问题，目前已解决全民经纪人cookie存取**** */' . "\r\n" . '	/* * *其他功能如用到cookie 存在跨域问题 请参照下面json格式的解析处理**** */' . "\r\n" . '	/* * *功能程序员端可用BaseAction 中$owndomain和$rget变量来判断是否是手机站独立部署请求的**** */' . "\r\n" . '	/* * *支持ajax的post和get请求**** */' . "\r\n" . '	define("REQUEST_METHOD", strtoupper(trim($_SERVER["REQUEST_METHOD"])));' . "\r\n" . '	define("IS_AJAX", ((isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") || !empty($_POST["ajax"]) || !empty($_GET["ajax"])) ? true : false);' . "\r\n" . '	$tjscript="' . $tjscript . '";  /**第三方统计js脚本**/' . "\r\n" . '	$token="' . session('token') . '";' . "\r\n" . '	$Request_url="";' . "\r\n" . '	$Request_site="' . $this->siteUrl . '";' . "\r\n" . '	if(isset($_SERVER["QUERY_STRING"]) && !empty($_SERVER["QUERY_STRING"])){' . "\r\n" . '		$Request_url = $Request_site."/index.php?".$_SERVER[\'QUERY_STRING\'] . "&rget=3&owndomain=" . $_SERVER[\'HTTP_HOST\'];' . "\r\n" . '	}else if($_SERVER["QUERY_STRING"]=="" || !strpos($_SERVER["REQUEST_URI"],"g=Wap")){' . "\r\n" . '		$Request_url=$Request_site."/index.php?g=Wap&m=Index&a=index&token=".$token."&rget=3&owndomain=" . $_SERVER[\'HTTP_HOST\'];' . "\r\n" . '	}else{' . "\r\n" . '		$REQUEST_Uri=preg_replace(\'/\\/(.*)\\?/i\',"/index.php?",$_SERVER["REQUEST_URI"]);  /***支持不带index.php URL请求***/' . "\r\n" . '		$Request_url = $Request_site . $REQUEST_Uri . "&rget=3&owndomain=" . $_SERVER[\'HTTP_HOST\'];' . "\r\n" . '	}' . "\r\n" . '	if(strpos($Request_url, "token=".$token) && strpos($Request_url, "g=Wap") && strpos($Request_url, "m=") && strpos($Request_url, "a=")){' . "\r\n" . '	if (REQUEST_METHOD == "POST") {' . "\r\n" . '		if(IS_AJAX) $_POST["ajax"]=1;' . "\r\n" . '		$responsearr = httpRequest($Request_url, REQUEST_METHOD, $_POST);' . "\r\n" . '		$tmpcontent=$responsearr["1"];' . "\r\n" . '	} else {' . "\r\n" . '		if(IS_AJAX) $Request_url=$Request_url."&ajax=1";' . "\r\n" . '		$responsearr = httpRequest($Request_url, REQUEST_METHOD, null);' . "\r\n" . '		/**get方式请求页面时**正则匹配换掉 图片 css js等引用文件用的相对地址成绝对地址**POST方式不需要替换**/' . "\r\n" . '		$tplstatic1REG=\'/(src|href)=([\\"\\\'])([\\.]{0,2}[\\/]?)tpl\\/static/i\';' . "\r\n" . '		$tplstatic2REG=\'/url\\(([\\"\\\'])([\\.]{0,2}[\\/]?)tpl\\/static/i\';' . "\r\n" . '		$tplWap1REG=\'/(src|href)=([\\"\\\'])([\\.]{0,2}[\\/]?)tpl\\/wap/i\';' . "\r\n" . '		$tplWap2REG=\'/url\\(([\\"\\\'])([\\.]{0,2}[\\/]?)tpl\\/wap/i\';' . "\r\n" . '		$tmpcontent=preg_replace($tplstatic1REG,"\\\\1=\\\\2".$Request_site."/tpl/static",$responsearr["1"]);' . "\r\n" . '		$tmpcontent=preg_replace($tplstatic2REG,"url(\\\\1".$Request_site."/tpl/static",$tmpcontent);' . "\r\n" . '		$tmpcontent=preg_replace($tplWap1REG,"\\\\1=\\\\2".$Request_site."/tpl/Wap",$tmpcontent);' . "\r\n" . '		$tmpcontent=preg_replace($tplWap2REG,"url(\\\\1".$Request_site."/tpl/Wap",$tmpcontent);' . "\r\n" . '' . "\r\n" . '		/*$tmpcontent=preg_replace($tplstatic1REG,"$1=$2".$Request_site."/tpl/static",$responsearr["1"]);' . "\r\n" . '		$tmpcontent=preg_replace($tplstatic2REG,"url($1".$Request_site."/tpl/static",$tmpcontent);' . "\r\n" . '		$tmpcontent=preg_replace($tplWap1REG,"$1=$2".$Request_site."/tpl/Wap",$tmpcontent);' . "\r\n" . '		$tmpcontent=preg_replace($tplWap2REG,"url($1".$Request_site."/tpl/Wap",$tmpcontent);*/' . "\r\n" . '	}' . "\r\n" . '	 /***Header("Access-Control-Allow-Origin:$Request_site");****/' . "\r\n" . '		/**ajax请求时 json封装带过来的数据 是否需要解析**/' . "\r\n" . '		/**格式为**{"analyze":1,"error":0,"msg":"opt_cookie","data":{"ckkey":"bfdhdfhdf","ckv":2,"expire":3600}}**这样的json**/' . "\r\n" . '		/**analyze为数字：指明是否需要解析 大于0的值时需要解析 0不需要解析，请不要写成布尔值****/' . "\r\n" . '		/**error为数字：指明一个状态**msg为字符串：指明操作**data为数据库：指明要操作的数据**/' . "\r\n" . '		/***目前仅支持cookie存取，如有需要以后再扩展此功能******/' . "\r\n" . '		$jsonREG=\'/^\\{[\\"\\\']analyze[\\"\\\']\\:\\d\\,[\\"\\\']error[\\"\\\']\\:\\d\\,[\\"\\\']msg[\\"\\\']\\:(.*)data[\\"\\\']\\:(.*)\\}\\}$/i\';' . "\r\n" . '		if(preg_match($jsonREG,$tmpcontent,$matches)){' . "\r\n" . '		   $jsonstr=$matches[0];' . "\r\n" . '		   $jsonarr=!empty($jsonstr)? json_decode($jsonstr,TRUE):false;' . "\r\n" . '		   if($jsonarr && is_array($jsonarr)){' . "\r\n" . '			  $is_analyze=isset($jsonarr["analyze"]) ? intval($jsonarr["analyze"]):0;' . "\r\n" . '			  if($is_analyze > 0){' . "\r\n" . '		       $tmpcontent=$jsonarr["error"];' . "\r\n" . '			   ' . "\r\n" . '			   switch($jsonarr["msg"]){' . "\r\n" . '				  case "opt_cookie":' . "\r\n" . '					  $tmpdata=$jsonarr["data"];' . "\r\n" . '					  $expire=intval($tmpdata["expire"]);' . "\r\n" . '					  $expire=$expire>0 ? time()+$expire :0;' . "\r\n" . '					  setcookie($tmpdata["ckkey"], urlencode(json_encode($tmpdata["ckv"])), $expire, "/",$_SERVER["HTTP_HOST"]);' . "\r\n" . '					  break;' . "\r\n" . '				default:' . "\r\n" . '					' . "\r\n" . '					break;' . "\r\n" . '				}' . "\r\n" . '		      }' . "\r\n" . '		   }' . "\r\n" . '		}' . "\r\n" . '	echo $tmpcontent; /*输出页面或数据*/' . "\r\n" . '	if (!IS_AJAX && !empty($tjscript)) {' . "\r\n" . '		$tjscript = base64_decode($tjscript);' . "\r\n" . '		echo $tjscript;' . "\r\n" . '	}' . "\r\n" . '	exit();' . "\r\n" . '	}else{' . "\r\n" . '		header("Content-type: text/html; charset=utf-8");' . "\r\n" . '		$tipsstr="<style type=\'text/css\'>.mytips{height: 60%;margin: 20px;width: 100%;} .mytips p{font-size: 18px;font-weight: bold;margin: 10px;padding-top: 10px;}</style>";' . "\r\n" . '		$tipsstr.="<div class=\'mytips\'><p>请求地址出错！</p></div>";' . "\r\n" . '		echo $tipsstr;' . "\r\n" . '	}' . "\r\n" . '' . "\r\n" . '	function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false) {' . "\r\n" . '		$Cookiestr=""; /**cUrl COOKIE处理**/' . "\r\n" . '		if(!empty($_COOKIE)){' . "\r\n" . '		   foreach($_COOKIE as $vk=>$vv){' . "\r\n" . '		       $tmp[]=$vk."=".$vv;' . "\r\n" . '		   }' . "\r\n" . '			$Cookiestr=implode(";",$tmp);' . "\r\n" . '		}' . "\r\n" . '		$ci = curl_init();' . "\r\n" . '		/* Curl settings */' . "\r\n" . '		curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);' . "\r\n" . '		curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");' . "\r\n" . '		curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间，如果设置为0，则无限等待 */' . "\r\n" . '		curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */' . "\r\n" . '		curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);' . "\r\n" . '		switch ($method) {' . "\r\n" . '			case "POST":' . "\r\n" . '				curl_setopt($ci, CURLOPT_POST, true);' . "\r\n" . '				if (!empty($postfields)) {' . "\r\n" . '					$tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;' . "\r\n" . '					curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);' . "\r\n" . '				}' . "\r\n" . '				break;' . "\r\n" . '			default:' . "\r\n" . '				curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */' . "\r\n" . '				break;' . "\r\n" . '		}' . "\r\n" . '		curl_setopt($ci, CURLOPT_URL, $url);' . "\r\n" . '		curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);' . "\r\n" . '		curl_setopt($ci, CURLINFO_HEADER_OUT, true);' . "\r\n" . '		curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr);/***COOKIE带过去***/' . "\r\n" . '		$response = curl_exec($ci);' . "\r\n" . '		$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);' . "\r\n" . '' . "\r\n" . '		if ($debug) {' . "\r\n" . '			echo "=====post data======\\r\\n";' . "\r\n" . '			var_dump($postfields);' . "\r\n" . '			echo "=====info===== \\r\\n";' . "\r\n" . '			print_r(curl_getinfo($ci));' . "\r\n" . '' . "\r\n" . '			echo "=====$response=====\\r\\n";' . "\r\n" . '			print_r($response);' . "\r\n" . '		}' . "\r\n" . '		curl_close($ci);' . "\r\n" . '		return array($http_code, $response);' . "\r\n" . '	}' . "\r\n" . '' . "\r\n" . '?>';
		$fname = 'index.php';
		header('Pragma: public');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Content-Type:application/force-download');
		header('Content-Type:application/octet-stream');
		header('Content-Type:application/download');
		header('Content-Disposition: attachment; filename=' . $fname);
		header('Content-Transfer-Encoding: binary');
		echo $content;
	}

	private function dexit($data = '')
	{
		if (is_array($data)) {
			echo json_encode($data);
		}
		else {
			echo $data;
		}

		exit();
	}
}

?>
